﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;


namespace NEWS.Admin
{
    public partial class PostForum : System.Web.UI.Page
    {
        private static string mode = "";
        private static ForumNewsBO forumnewsbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mode = Request.QueryString["id"];
                drldanhmuc.DataSource = CategoryInfo.GetCategoryInfo_BySingle(false, true, false, false,true);
                drldanhmuc.DataBind();                
                if (mode == null)
                {                    
                    pn_iduser.Visible = false;
                    pn_ngay.Visible = false;
                    pn_solanxem.Visible = false;
                    getSubTopicForum(Convert.ToInt32(drldanhmuc.SelectedValue));
                }
                else 
                {
                    btndangtin.Text = "Cập nhật bài viết";
                    lblheader.Text = "Chỉnh sửa bài viết";
                    pn_iduser.Visible = false;
                    pn_ngay.Visible = false;
                    pn_solanxem.Visible = false;
                    forumnewsbo = ForumNewsBO.GetForumNewsBO(Convert.ToInt32(mode));
                    txtforumnewsid.Text = forumnewsbo.ForumNewID.ToString();
                    txtusername.Text = forumnewsbo.UserName;
                    //dldanhmuccon.SelectedValue = forumnewsbo.SubTopicForumID.ToString();
                    SubTopicForumBO subTopic = SubTopicForumBO.GetSubTopicForumBO(Convert.ToInt32(forumnewsbo.SubTopicForumID));
                    drldanhmuc.SelectedValue = subTopic.CategoryID.ToString();
                    getSubTopicForum(Convert.ToInt32(drldanhmuc.SelectedValue));
                    dldanhmuccon.SelectedValue = forumnewsbo.SubTopicForumID.ToString();
                    //string tendanhmuc = dldanhmuccon.SelectedValue;
                    txttitle.Text = forumnewsbo.Title;
                    txtcontent.Text = forumnewsbo.ForumNewsContent;
                }
             }
        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            if (dldanhmuccon.SelectedValue.Trim() != "")
            {
                if (mode == null)
                {
                    forumnewsbo = ForumNewsBO.NewForumNewsBO();
                    forumnewsbo.UserName = "admin";
                    forumnewsbo.Title = txttitle.Text;
                    forumnewsbo.SubTopicForumID = Convert.ToInt32(dldanhmuccon.SelectedValue);
                    forumnewsbo.CreatedDate = DateTime.Now;
                    forumnewsbo.UpdatedDate = DateTime.Now;
                    forumnewsbo.ForumNewsContent = txtcontent.Text;
                    forumnewsbo.ViewCount = 0;
                    try
                    {
                        forumnewsbo.Save();
                        message.Text = "Thêm thành công";
                        Response.Redirect("Forums.aspx?Save=Complete");
                    }
                    catch
                    {
                        message.Text = "Thêm thất bại";
                    }
                }
                else
                {
                    //newsbo = NewsBO.GetNewsBO(newsid);
                    int forumnewid = Convert.ToInt32(txtforumnewsid.Text);
                    forumnewsbo = ForumNewsBO.GetForumNewsBO(forumnewid);
                    forumnewsbo.Title = txttitle.Text;
                    forumnewsbo.ForumNewsContent = txtcontent.Text;
                    forumnewsbo.CreatedDate = DateTime.Now;
                    forumnewsbo.UpdatedDate = DateTime.Now;
                    forumnewsbo.SubTopicForumID = Convert.ToInt32(dldanhmuccon.SelectedValue);
                    try
                    {
                        forumnewsbo.Save();
                        message.Text = "Cập nhật thành công !";

                    }
                    catch
                    {
                        message.Text = "Cập nhật thất bại !";
                    }

                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chủ đề chưa được chọn !!!')", true);
                btndangtin.Focus();
                return;
            }
        }

        protected void drldanhmuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            getSubTopicForum(Convert.ToInt32(drldanhmuc.SelectedValue));
        }

        private void getSubTopicForum(int categoryid)
        {
            try
            {
                var subtopic = SubTopicForumInfo.GetSubTopicForumInfo_ByIDParrent(categoryid,true);
                dldanhmuccon.DataSource = subtopic;
                dldanhmuccon.DataBind();                
            }
            catch (Exception)
            {}
        }

    }
}

